home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / brainw32.zip / README.TXT
Text File  |  1994-04-29  |  30KB  |  757 lines

  1. ------------------------------------------------------------------------------
  2. Code distribution for the book:
  3. "Win32 System Services: The Heart of Windows NT"
  4. by Marshall Brain
  5. Published by Prentice Hall, ISBN 0-13-097825-6
  6. To order a copy of the book call 1-800-947-7700
  7.  
  8. Copyright 1994, by Prentice Hall.
  9.  
  10. Version 1.1, April 29, 1994.
  11. ------------------------------------------------------------------------------
  12.  
  13. INTRODUCTION -
  14.    The "code" directory contains all of the code found in the book "Win32
  15. System Services: The Heart of Windows NT", by Marshall Brain, published by 
  16. Prentice Hall. This code is being distributed so that you can work with the 
  17. examples in the book without having to retype them. 
  18.    If you have any questions or comments, you can send them to the
  19. following email address: "comments@iftech.com". Additional source
  20. code, examples, and corrections are also available by email by
  21. sending an empty message to "info@iftech.com". These are internet
  22. email addresses--you can reach the internet through Compuserve,
  23. MCI mail, and many other services. Consult your specific mail service
  24. for details. 
  25.    The following copyright notice applies to all information found
  26. in this distribution:
  27.  
  28.     Copyright 1994, by Interface Technologies, Inc.
  29.  
  30. You are free to copy and distribute this package for personal,
  31. non-commercial uses provided that this readme file and copyright
  32. notice remain with the package. There is no warranty, either 
  33. expressed or implied, supplied with any of the code or information 
  34. found in this distribution.
  35.  
  36.  
  37. UNPACKING THE ZIP FILE -
  38.    When you unzip this distribution, be sure that you use the "-d"
  39. option so that the directory structure gets restored correctly.
  40.  
  41.  
  42. COMPILING THE CODE -
  43.    Each directory on this disk has its own makefile. Many of the
  44. directories contain multiple source files and only one makefile--in 
  45. these directories, you can edit the APP variable to compile
  46. each different code file (see individual makefiles for examples). 
  47.    Before you can use the makefiles, you need to set some environment 
  48. variables. This is most easily done by opening a command shell and 
  49. typing "\mstools\setenv" (search for "setenv.bat" if you have trouble 
  50. finding the file). This batch file sets specific environment variables 
  51. to needed values.
  52.    Each directory has its own README.TXT file. Read this file for
  53. special compilation instructions before trying to compile or run
  54. the code.
  55.    To compile the code, cd into a specific code directory and type
  56. "nmake". Compilation is automatic. If you encounter problems either 
  57. in finding things or compiling the code, it is probably because
  58. Microsoft changed or moved things. Send mail to the "info" address
  59. given above to find updated compilation instructions.
  60.    More detailed compiling instructions are given at the bottom of this
  61. file in "Appendix A".
  62.  
  63. NOTE -
  64.     To get some of the MFC code to compile we had to modify one of the
  65.     /D options in ntsample.mak. Presumably this will get fixed 
  66.     at some point but in case it doesn't, open ntsample.mak
  67.     and search for "/DWINVER=0x0300" and change it to
  68.     "/DWINVER=0x030A". This will cause the correct function
  69.     in one of the MFC header files to be included properly.
  70.  
  71.  
  72. OTHER BOOKS IN THE SERIES
  73.    "Win32 System Services: The Heart of Windows NT" is the fourth
  74. of a five book series on Windows NT programming and administration. 
  75. Other books in the series include:
  76.  
  77.     - "Windows NT Programming: An Introduction Using C++" 
  78.       ISBN 0-13-097833-7
  79.  
  80.     - "Using Windows NT: The Essentials for Professionals" 
  81.       ISBN 0-13-091977-2
  82.  
  83.     - "Windows NT Administration: From Single Systems to 
  84.       Heterogeneous Networks" ISBN 0-13-176694-5
  85.  
  86.     - "Win32 System Services: The Heart of Windows NT"
  87.       ISBN 0-13-097825-6
  88.  
  89. To order a copy of any of these books, go through a local bookstore or
  90. order by mail from Prentice Hall by calling 1-800-947-7700.
  91.  
  92.  
  93. ========================================================================
  94.  
  95.  
  96. FROM THE PREFACE OF "WIN32 SYSTEM SERVICES: THE HEART OF WINDOWS NT"
  97.  
  98. The Win32 system services are the innovative, cutting-edge 
  99. capabilities that make Windows NT interesting and exciting. Amid all of 
  100. the media attention surrounding NT, you have probably heard and read 
  101. about many of the modern capabilities that this new operating system 
  102. contains:
  103.  
  104.     Processes and threads
  105.     Synchronization
  106.     Remote Procedure Calls (RPCs)
  107.     Event logging
  108.     Network communications
  109.     TCP/IP networking with UNIX and OS/2 machines
  110.     File mapping
  111.     Interprocess communication
  112.     Compressed file access
  113.     Network drive and printer sharing
  114.     Serial communications
  115.     Services (background processing like UNIX daemons)
  116.     Object and file security
  117.  
  118. The goal of this book is to show you how to use all of these 
  119. different services in your own applications. This book contains 
  120. hundreds of concise, clear, and easy-to-understand example programs 
  121. and explanations. The examples show you how to access the system 
  122. services listed above, as well as many others. You can use these 
  123. examples directly to understand the concepts. You can also drop the 
  124. sample code into your own programs and then extend it to give your 
  125. applications new and useful features.
  126.  
  127. By learning about the many and varied system services available 
  128. inside of Windows NT, you can make your programs much more 
  129. interesting to the user. For example, if you learn about threads and then 
  130. add multiple threads to your applications, the user gets a much 
  131. smoother interface (see Chapter 5). Your program will also take 
  132. advantage of multiple processors on machines that have them. If you 
  133. add network support to your applications, then the user is able to 
  134. access network resources that are unavailable on a single machine (see 
  135. Chapter 7). If you add modem support, then you can use it to let the 
  136. user dial into a remote system (see Chapter 12). Or you might use a 
  137. modem to let the user dial a support line or registration system for a 
  138. product that your company sells.
  139.  
  140. The goal of this book is to give you a thorough and clear 
  141. understanding of the system services so that you can take advantage of 
  142. all of the capabilities that Windows NT has to offer. 
  143.  
  144. AUDIENCE
  145. This book is designed for two types of people. If you are a 
  146. devoted connoisseur or student of programming, the study of the 
  147. system functions is interesting in its own right. It's fun to see what is 
  148. available, and to try to think of ways to exploit those capabilities. One 
  149. way to use this book is to simply browse through it and see what is 
  150. possible. You can work with the examples and extend them in ways 
  151. that you find enjoyable. 
  152.  
  153. If, on the other hand, you are a programmer with a deadline, 
  154. enjoyable entertainment is probably not your goal. You may have 
  155. looked at the huge number of functions in the Windows API and found 
  156. it discouraging. Where do you start? What you want is a set of useful 
  157. examples and clear explanations that quickly solve your specific 
  158. problems. You will find this book useful because it organizes concepts 
  159. logically and provides the material that you need to master a specific 
  160. topic very quickly.
  161.  
  162. If you are migrating from another operating system to Windows 
  163. NT, this book will help you to quickly map your existing knowledge into 
  164. the NT framework. See Chapter 1 for a list of the 100 most common 
  165. programming questions in Windows NT as well as the sections that 
  166. contain the answers.
  167.     
  168. ORGANIZATION
  169. This book is organized by functional categories. For example, 
  170. Chapter 2 talks about all of the functions that pertain to File I/O and file 
  171. handling. Chapter 3 deals with disk drives. Chapter 4 discusses 
  172. directories. Chapter 5 talks about processes and threads. And so on. 
  173. You will find that each chapter starts with a general overview of the 
  174. topic, followed by sections that describe and give examples for subsets 
  175. of functions. 
  176.  
  177. In many chapters you will find integrating examples that combine 
  178. different capabilities from several chapters to create larger applications. 
  179. Many of these larger examples form complete applications that you can 
  180. use as starting points for new projects of your own.
  181.  
  182. PHILOSOPHY
  183. Several principles guide the design of this book. The first principle 
  184. is the most important and is therefore woven throughout: it is 
  185. simplicity. There is nothing worse than trying to look up a function, 
  186. only to find that its example is embedded within sample code that takes 
  187. three days to comprehend because it is 28 pages long. In all cases 
  188. functions are first demonstrated in code that is as simple as possible so 
  189. that you can extract the essence of each one very quickly. Then they 
  190. may also be integrated into larger examples. In many cases you will 
  191. look at the example code and say to yourself, "This is trivial!". That is 
  192. exactly the intent. If the book makes everything look simple and easy 
  193. for you, then it has accomplished its goal.
  194.  
  195. The second principle found in this book revolves around the idea 
  196. of functional families. Functions in the 32-bit API very rarely exist on 
  197. their own--it is far more common to find small collections of functions 
  198. that relate very closely to one another. For example, the ReadFile 
  199. function reads from a file, but you must open the file with CreateFile 
  200. before you can read from it, and then remember to close it with 
  201. CloseHandle afterwards (see Chapter 2). These functions belong to a 
  202. family. In this book you will generally find small groupings of functions 
  203. described as logical units.
  204.  
  205. The third principle in this book is currency. The Windows 
  206. API has been around for some time, and when you look at it you will 
  207. find that there is a certain layering. The documentation will often say 
  208. things like, "this function is retained for compatibility with version 1.8, 
  209. but has been superseded by function xyz." This book deals only with 
  210. the current functions, and leaves the superseded ones to die in peace.
  211.  
  212. The last principle guiding this book is independence. Each chapter 
  213. in this book is as stand-alone as possible, so that you can jump around 
  214. in the book as you please. In cases where material from other chapters 
  215. is important, you will find references to the related sections. 
  216.  
  217. PREREQUISITES AND LANGUAGES
  218. This book makes no assumptions about former knowledge of 
  219. systems programming in Windows or in any other system. It's only 
  220. real assumption is that you know C and/or C++. You will find that the 
  221. example code divides into two categories:
  222.  
  223. 1) Text examples that run in "console mode". These programs run 
  224. from the MS-DOS prompt, accept keyboard input, and produce 
  225. text output. They form the vast majority of the code in this book 
  226. because they are simple. These programs are C++ compatible, 
  227. but the only elements really being used from C++ are the I/O 
  228. stream libraries.
  229.  
  230. 2) "Windows" programs that use the Windows GUI. These 
  231. programs use C++ and the Microsoft Foundation Class library 
  232. (MFC). Book 2 in this series, "Windows NT Programming: An 
  233. Introduction using C++" describes how to create programs with 
  234. MFC, and will be helpful to you if you in understanding these 
  235. examples if you are not already familiar with MFC.
  236.  
  237. If you are a C programmer with no C++ experience, you will 
  238. have no trouble understanding the console programs. The only unusual 
  239. thing you will see is code that looks like this:
  240.  
  241.     char s[100];
  242.  
  243.     // Prompt the user
  244.     cout << "Enter a line of text: ";
  245.     // Read a line of text from the user
  246.     cin.getline(s,100);
  247.     // Echo the user's input to stdout
  248.     cout << s << endl;
  249.  
  250. This code declares a character string s, and then uses "cout <<" 
  251. to output a prompt to the user. The "cin.getline" statement reads a line 
  252. of text from the user. The final "cout" statement echos the line. The 
  253. book "Windows NT Programming: An Introduction using C++"  
  254. contains an extensive C++ appendix to help you get started with the 
  255. language if you are interested. It will also help you to understand the 
  256. MFC code.
  257.  
  258. CONTENTS OF THE BOOK
  259. Each chapter in this book talks about a different service in the 32-
  260. bit API. Chapter 1 contains a list of the 100 most common questions  
  261. about the system services, and it will help you to very quickly find 
  262. material that interests you. The list below summarizes the different 
  263. chapters in the book to help you with browsing.
  264.  
  265. o Chapter 1 is an introduction. It contains a list of the 100 most 
  266. common programming questions about the Windows NT system 
  267. services, and this list will serve as a good road map for you. The 
  268. chapter also contains general information that is useful throughout 
  269. the book.
  270.  
  271. o Chapter 2 discusses Files. It shows you how to open, read and 
  272. write files, how to read from compressed files, how to map files 
  273. into memory, and so on. The 32-bit API contains quite a few very 
  274. interesting file capabilities.
  275.  
  276. o Chapter 3 talks about the directory structure. It shows you how 
  277. to create and traverse directory trees.
  278.  
  279. o Chapter 4 introduces the drive structure of Windows NT. You 
  280. will learn how to query the drives on your local machine, and also 
  281. how to query and connect to drives on the network.
  282.  
  283. o Chapter 5 offers a complete introduction to processes and 
  284. threads in the NT environment. You will see how to multithread 
  285. your own applications, and the chapter shows you how to multi-
  286. thread MFC applications as well. There is also a discussion of 
  287. processes, inter-process communication, and inheritance.
  288.  
  289. o Chapter 6 discusses NT's synchronization mechanisms. Critical 
  290. sections, mutexes, semaphores and events help you to write 
  291. multi-threaded code that does not step on itself. The chapter 
  292. introduces you to general synchronization principles and bugs, and 
  293. shows solutions to a number of standard problems. It also shows 
  294. you how to incorporate synchronization mechanisms into an MFC 
  295. program without stalling its event loop.
  296.  
  297. o Chapter 7 talks about network communications. NT contains 
  298. two different native technologies for communicating over the 
  299. network: mailslots and named pipes. NT also supports UPD and 
  300. TCP packets on TCP/IP networks. The latter capabilities let you 
  301. intercommunicate with UNIX and other TCP/IP machines. The 
  302. chapter includes a complete introduction to network principles and 
  303. terminology. 
  304.  
  305. o Chapter 8 talks about Remote Procedure Calls, or RPCs. An RPC 
  306. lets you make a function call that is transmitted over the network 
  307. and executed on another machine. This chapter contains a 
  308. complete introduction to the hows and whys of RPCs, as well as 
  309. design principles to keep in mind when creating client/server 
  310. systems with RPCs. It contains examples of auto, implicit, explicit 
  311. and context binding, and also shows how to incorporate RPCs into 
  312. MFC programs.
  313.  
  314. o Chapter 9 discusses NT services. These are background 
  315. processes that start at boot time and run regardless of who is 
  316. logged in, just like daemons in UNIX. This chapter shows you how 
  317. to create your own services and install them in NT's service 
  318. manager. It also shows you how to place RPC servers into 
  319. services so they run continuously.
  320.  
  321. o Chapter 10 offers a complete introduction to the NT security 
  322. system. This system is uniform across all NT objects, so you learn 
  323. how to secure files, registry keys, pipes, semaphores, etc. It 
  324. teaches you everything you need to know to create and modify 
  325. access control lists and security descriptors.
  326.  
  327. o Chapter 11 discusses consoles, which you will use when you 
  328. want to create text-based programs in Windows NT. For example, 
  329. if you want to create a terminal emulator, consoles offer an easy 
  330. way to handle the keyboard input and character output for the 
  331. emulator.
  332.  
  333. o Chapter 12 talks about communications ports in NT systems. It 
  334. shows you how to access both the serial and parallel ports, and 
  335. demonstrates a simple terminal emulator and bulletin board 
  336. system.
  337.  
  338. o Chapter 13 shows you how to access system information, and 
  339. how to log users off and shutdown or reboot the system. For 
  340. example, this chapter shows you how to determine how many 
  341. CPUs a system contains, or how many buttons there are on the 
  342. mouse.
  343.  
  344. o Chapter 14 shows you how to modularize your programs using 
  345. dynamic link libraries (DLLs). Windows NT can be thought of as an 
  346. extensible operating system because of DLLs. A programmer can 
  347. easily add capabilities that others can use by creating a DLL and 
  348. publishing its interface. NT itself places much of its functionality in 
  349. DLLs.
  350.  
  351. o Chapter 15 contains short discussions and examples on six 
  352. miscellaneous topics: the registry, the event log, the NT time 
  353. model, error handling, structured exception handling, and the 
  354. memory model.
  355.  
  356. o Appendix A shows you how to compile the different types of 
  357. code found in the book.
  358.  
  359. o Appendix B contains information on contacting the authors via 
  360. Email to ask questions and retrieve free supplements and updates 
  361. to this book.
  362.  
  363.  
  364. ========================================================================
  365.  
  366.  
  367. FROM Appendix A AND B OF "WIN32 SYSTEM SERVICES: THE HEART OF WINDOWS NT"
  368.  
  369.  
  370. Appendix A 
  371. Compiling the Code in this Book 
  372.  
  373. This book contains hundreds of listings. You will find that these 
  374. listings break down into four different categories: 
  375.  
  376. 1) Pure console programs that read and write text data 
  377.  
  378. 2) GUI programs using the 32-bit API 
  379.  
  380. 3) GUI programs using MFC 
  381.  
  382. 4) Other programs: DLL code, RPC code, etc. 
  383.  
  384. This appendix contains information on compiling the first three 
  385. types of code using the Win32 SDK or Visual C++ for Windows NT. The 
  386. fourth type of code is covered when the code appears. For example, the 
  387. RPC chapter contains makefiles for using the MIDL compiler and 
  388. compiling RPC programs. You compile these programs using the same 
  389. techniques that you use to compile the first three categories of code. 
  390. The supplied diskette contains appropriately adjusted makefiles 
  391. for the code in each directory. This saves you the trouble of creating your 
  392. own. Look through this appendix to learn about how those makefiles 
  393. work and how to use them. 
  394. If you have compilation problems, be sure to contact the 
  395. information server described in Appendix B to obtain free updates. 
  396.  
  397. A.1 COMPILING WITH THE WIN32 SDK 
  398. The code in Listing A.1 represents a typical console program 
  399. named textsamp.cpp. The makefile in Listing A.2 will compile it. Note 
  400. that the makefile's first line reads "APP=textsamp". Change the name 
  401. in this line to compile different programs.  
  402.  
  403. // textsamp.cpp 
  404.  
  405. #include <windows.h> 
  406. #include <iostream.h> 
  407.  
  408. void main(void) 
  409.   int x; 
  410.  
  411.   for (x=0; x<10; x++) 
  412.   { 
  413.     cout << "Hello" << endl; 
  414.     Beep(100, 50); 
  415.     Sleep(250); 
  416.   } 
  417.  
  418. Listing A.1 
  419. A simple console program (textsamp.cpp) 
  420.  
  421. APP=textsamp 
  422. NODEBUG=1 
  423.  
  424. !include <ntwin32.mak> 
  425.  
  426. $(APP).exe: $(APP).obj 
  427.     $(link) $(conlflags) $(ldebug) \ 
  428.       $(conlibsmt) $(APP).obj -out:$(APP).exe 
  429.  
  430. $(APP).obj: $(APP).cpp 
  431.     $(cc) $(cflags) $(cdebug) \ 
  432.       $(cvarsmt) $(APP).cpp 
  433.  
  434. Listing A.2 
  435. A makefile for Listing A.1 (textsamp.cpp).  Run mstools\setenv.bat before trying to 
  436. compile, and then type "nmake" to compile. Change the first line to compile a different 
  437. file. 
  438.  
  439. Before you can compile Listing A.1 with the makefile in Listing 
  440. A.2, you must set some environment variables. The easiest way to do 
  441. this is to run the file setenv.bat in the mstools directory. You might 
  442. also consider looking at which variables are set by setenv, and then 
  443. setting them permanently in your account using the System applet in 
  444. the Control Panel. Once the environment variables are set, create a new 
  445. directory, place Listings A.1 and A.2 in it, and then type "nmake" to 
  446. compile the code.  
  447. Note that in many cases you will have to add extra libraries to the 
  448. link line of the makefile. The library advapi32.lib is especially 
  449. popular. If your program will not link, expect missing libraries. For 
  450. example, to add the advanced API library to the makefile, change the 
  451. link line in the makefile so that it looks like this: 
  452.  
  453. $(APP).exe: $(APP).obj 
  454.     $(link) $(conlflags) $(ldebug) advapi32.lib \ 
  455.       $(conlibsmt) $(APP).obj -out:$(APP).exe 
  456.  
  457. Certain programs in Chapters 3 and 4 also want to link to 
  458. mpr.lib. The mstools\lib directory contains all of the normal system 
  459. libraries: Use qgrep in the lib directory with the "-l" option to find which 
  460. libraries contain functions that you have trouble locating. 
  461. Also note that the makefile uses "convarsmt" and "conlibsmt". 
  462. These are multi-threaded versions of the libraries, and they guarantee 
  463. that multi-threaded code will work correctly. 
  464. To compile GUI applications created using the 32-bit API (as 
  465. opposed to MFC programs, which are described below), use the makefile 
  466. shown in Listing A.4. Listing A.3 contains winsamp.cpp, which 
  467. represents a typical GUI application with a winmain main function. It 
  468. simply displays a message box and quits. The "ranmon" programs in 
  469. Chapter 7 need this makefile. 
  470.  
  471. // winsamp.cpp 
  472.  
  473. #include <windows.h> 
  474. #include <iostream.h> 
  475.  
  476. int WINAPI WinMain(HANDLE ghInstance,  
  477.   HANDLE hPrevInstance, LPSTR lpCmdLine, 
  478.   int nCmdShow) 
  479.   Beep (100, 100); 
  480.   MessageBox(NULL, "Sample message", 
  481.     "Winsamp", MB_OK | MB_ICONHAND); 
  482.   return 0; 
  483.  
  484. Listing A.3 
  485. A typical Windows NT GUI program (winsamp.cpp) 
  486.  
  487. APP=winsamp 
  488. NODEBUG=1 
  489.  
  490. !include <ntwin32.mak> 
  491.  
  492. $(APP).exe: $(APP).obj 
  493.     $(link) $(guilflags) $(ldebug) \ 
  494.       $(guilibsmt) $(APP).obj -out:$(APP).exe 
  495.  
  496. $(APP).obj: $(APP).cpp 
  497.     $(cc) $(cflags) $(cdebug) \ 
  498.       $(cvarsmt) $(APP).cpp 
  499.  
  500. Listing A.4 
  501. A makefile for Listing A.3 
  502.  
  503. As with the console program, create a new directory. Place in it 
  504. Listing A.3 with the file name "winsamp.cpp" and Listing A.4 with the 
  505. file name "makefile". Type "nmake" to compile the code. 
  506. This book contains a number of MFC (Microsoft Foundations Class 
  507. library) programs. Listing A.5 represents a typical MFC program, and 
  508. Listing A.7 is its makefile. Listing A.6 is a DEF file for the program, 
  509. and is required.  
  510.  
  511. // mfcsamp.cpp 
  512.  
  513. #include <afxwin.h> 
  514.  
  515. #define IDB_BUTTON 100 
  516.  
  517. // Declare the application class 
  518. class CButtonApp : public CWinApp 
  519. public: 
  520.     virtual BOOL InitInstance(); 
  521. }; 
  522.  
  523. // Create an instance of the application class 
  524. CButtonApp ButtonApp;   
  525.  
  526. // Declare the main window class 
  527. class CButtonWindow : public CFrameWnd 
  528. {  
  529.     CButton *button; 
  530. public: 
  531.     CButtonWindow(); 
  532.     ~CButtonWindow(); 
  533.     afx_msg void HandleButton(); 
  534.  
  535.     DECLARE_MESSAGE_MAP()      
  536. }; 
  537.  
  538. // The message handler function 
  539. void CButtonWindow::HandleButton() 
  540.     Beep(700,500); 
  541.  
  542. // The message map 
  543. BEGIN_MESSAGE_MAP(CButtonWindow, CFrameWnd) 
  544.     ON_COMMAND(IDB_BUTTON, HandleButton) 
  545. END_MESSAGE_MAP() 
  546.  
  547. // The InitInstance function is called once 
  548. // when the application first executes 
  549. BOOL CButtonApp::InitInstance() 
  550.     m_pMainWnd = new CButtonWindow(); 
  551.     m_pMainWnd->ShowWindow(m_nCmdShow); 
  552.     m_pMainWnd->UpdateWindow(); 
  553.     return TRUE; 
  554.  
  555. // The constructor for the window class 
  556. CButtonWindow::CButtonWindow() 
  557. {  
  558.     CRect r; 
  559.  
  560.     // Create the window itself 
  561.     Create(NULL,  
  562.         "CButton Tests",  
  563.         WS_OVERLAPPEDWINDOW, 
  564.         CRect(0,0,200,200)); 
  565.  
  566.     // Get the size of the client rectangle 
  567.     GetClientRect(&r); 
  568.     r.InflateRect(-20,-20); 
  569.  
  570.     // Create a button 
  571.     button = new CButton(); 
  572.     button->Create("Push me", 
  573.         WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
  574.         r, 
  575.         this, 
  576.         IDB_BUTTON); 
  577.  
  578. // The destructor for the window class 
  579. CButtonWindow::~CButtonWindow() 
  580.     delete button; 
  581.     CFrameWnd::~CFrameWnd(); 
  582.  
  583. Listing A.5 
  584. A typical MFC program (mfcsamp.cpp) 
  585.  
  586. NAME         mfcsamp 
  587. DESCRIPTION  'demo code for MFC' 
  588.  
  589. EXETYPE      WINDOWS 
  590. STUB         'WINSTUB.EXE' 
  591.  
  592. CODE         PRELOAD MOVEABLE DISCARDABLE 
  593. DATA         PRELOAD MOVEABLE MULTIPLE 
  594.  
  595. HEAPSIZE     1024 
  596. STACKSIZE    4096 
  597.  
  598. Listing A.6 
  599. A DEF file for the MFC program (mfcsamp.def) 
  600.  
  601. # Change this path to match your system 
  602. !include c:\mstools\mfc\samples\ntsample.mak 
  603.  
  604. # Change the following name to compile  
  605. # different files 
  606. FILENAME=mfcsamp 
  607.  
  608. OBJS=$(FILENAME).obj 
  609.  
  610. $(FILENAME).exe: $(OBJS) $(FILENAME).def  
  611.     $(LINK) $(GUIFLAGS) -out:$(FILENAME).exe \ 
  612.       $(FILENAME).obj $(MFCLIB) $(GUILIBS) 
  613.  
  614. Listing A.7 
  615. A makefile for the MFC program 
  616.  
  617. Note that the makefile includes "ntsample.mak". Adjust the path 
  618. to this file to match the path on your machine.  
  619. To compile the code, create a new directory. Copy Listing A.5 into a 
  620. file named "mfcsamp.cpp". Copy Listing A.6 into a file named 
  621. "mfcsamp.def". Copy Listing A.7 into a file named "makefile". Type 
  622. "nmake" to compile the code. When you create programs of your own, 
  623. adjust the FILENAME value in the makefile and change the first two 
  624. lines of the DEF file. 
  625. Many of the larger MFC programs in this book also use an RC file 
  626. to handle their menus. The appropriate makefile for compiling these 
  627. MFC programs is shown in Listing A.8. 
  628.  
  629. # Change this path to match your system 
  630. !include c:\mstools\mfc\samples\ntsample.mak 
  631.  
  632. # Change the following name to compile  
  633. # different files 
  634. FILENAME=mandel2 
  635.  
  636. OBJS=$(FILENAME).obj 
  637.  
  638. $(FILENAME).exe: $(OBJS) mandel.res mandel.def  
  639.     $(LINK) $(GUIFLAGS) -out:$(FILENAME).exe \ 
  640.       $(FILENAME).obj $(MFCLIB) $(GUILIBS) \ 
  641.       mandel.res 
  642.  
  643. Listing A.8 
  644. A makefile for MFC programs that use RC files. 
  645.  
  646. This makefile compiles the mandelbrot examples in Chapter 5. The 
  647. two references to "mandel.res" cause the makefile to compile 
  648. "mandel.rc". Change the name "mandel" to the name of your RC file to 
  649. compile other RC files. See Chapter 6 of Windows NT Programming: An 
  650. Introduction Using C++ for more information. 
  651.  
  652. A.2 COMPILING WITH VISUAL C++ 
  653. You can compile the code in this book using Visual C++ either by 
  654. creating new project files, or by loading the makefiles supplied on the 
  655. diskette as external makefiles. The latter option is much easier, so it is 
  656. discussed here. 
  657. Listing A.1 in the previous section represents a simple console 
  658. program. Listing A.2 is its makefile. You can either type these two files 
  659. in or copy them from the diskette that came with the book. To compile 
  660. this code under Visual C++ take the following steps: 
  661.  
  662. 1) Start Visual C++. 
  663.  
  664. 2) Since Visual C++ remembers any previously open project and its 
  665. files, go into the Project menu and choose the Close option if it is 
  666. available. Go into the File menu and choose the Close option as 
  667. many times as you are allowed. This clears out the old project and 
  668. its files. 
  669.  
  670. 3) Select the Open option in the Project menu. In this dialog, 
  671. change to the directory that contains the code contained in Listings 
  672. A.1 and A.2. Type "makefile." into the File Name field (or choose 
  673. "All Files" from the File Type list and double-click on "makefile").  
  674.  
  675. 4) You will see a dialog asking if you want to open the makefile as 
  676. an external makefile. Answer "Yes".  
  677.  
  678. 5) You will see an Options dialog. Enter a name for the EXE file in 
  679. the first field. Use the value from the APP variable (e.g. use 
  680. "textsamp.exe" here).  
  681.  
  682. 6) The code should compile without error using the Build option in 
  683. the Project menu. Run it with the Execute option in the Project 
  684. menu. 
  685.  
  686. Follow the same steps for the other example files seen in Section 
  687. A.1.  
  688. To compile multiple files in the same directory, close the current 
  689. project using the Close option in the Project directory. Change the 
  690. APP or FILENAME value in the makefile. Load the modified makefile 
  691. using the Open option in the Project menu. In the Options dialog, set 
  692. the EXE name to match the FILENAME.  
  693. You should be able to compile all of the code presented in this book 
  694. using Visual C++, with the possible exception of the RPC code in 
  695. Chapter 8. Visual C++ for NT version 2.0 does not contain the MIDL 
  696. compiler, so there is no way to compile the IDL files. The SDK contains 
  697. the MIDL file however (midl.exe), and if you borrow it and place it in the 
  698. msvcnt\bin directory, you can compile RPC code without problems.  
  699.  
  700. ========================================================================
  701.  
  702. Appendix B 
  703. Contacting the Author 
  704.  
  705. Windows NT, like all operating systems, will change over time. We 
  706. want to keep your copy of the book current by providing you with 
  707. correction and update pages via electronic mail. If you find errors in the 
  708. book, or if you have any questions, comments, or suggestions for 
  709. improving the book, we would like to hear from you. Your comments will 
  710. help us improve later editions, and we'll post your corrections so other 
  711. readers can take advantage of them. 
  712. You can contact the author either by US mail or by electronic mail. 
  713. Electronic mail is preferred. The US mail address is: 
  714.  
  715. Interface Technologies 
  716. P.O. Box 841 
  717. Zebulon, NC 27597 
  718.  
  719. To send suggestions, comments, or corrections via electronic mail, 
  720. address e-mail to: 
  721.  
  722. comments@iftech.com 
  723.  
  724. To ask a question, send e-mail to: 
  725.  
  726. questions@iftech.com 
  727.  
  728. To request a list of the available update and correction pages, as 
  729. well as supplements, send e-mail to: 
  730.  
  731. info@iftech.com 
  732.  
  733. In the last case, the message can contain anything or nothing at 
  734. all. The mail system will send an automated reply with a list of topics 
  735. and further instructions. 
  736. These e-mail addresses are on the Internet and will suffice on mail 
  737. systems connected directly to the Internet. If you are using Compuserve, 
  738. you can add the prefix "INTERNET:" to the address to get your message 
  739. onto the Internet. For example: 
  740.  
  741. INTERNET:comments@iftech.com 
  742.  
  743. Other mail services such as MCImail, as well as many PC bulletin 
  744. boards, also offer Internet access. See the documentation for your 
  745. specific system for details.